- Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path139. Word Break.c
67 lines (45 loc) · 1.56 KB
/
139. Word Break.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
UPDATE (2017/1/4):
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
*/
boolsearch(char*s, intl, char**wordDict, intwordDictSize) {
inti;
char*word;
for (i=0; i<wordDictSize; i++) {
word=wordDict[i];
if (word&&l==strlen(word) && !strncmp(s, word, l)) return true;
}
return false;
}
boolwordBreak(char*s, char**wordDict, intwordDictSize) {
inti, j, l;
bool*buff, *e, result;
l=strlen(s);
buff=calloc((l+1), sizeof(bool));
//assert(buff);
buff[0] = true;
e=&buff[1];
for (i=0; i<l; i++) {
for (j=i; !e[i] &&j >= 0; j--) {
e[i] =e[j-1] &&search(&s[j], i-j+1, wordDict, wordDictSize);
}
}
result=e[l-1];
free(buff);
returnresult;
}
/*
Difficulty:Medium
Total Accepted:156.4K
Total Submissions:523.6K
Companies Google Uber Facebook Amazon Yahoo Bloomberg Pocket Gems
Related Topics Dynamic Programming
Similar Questions
Word Break II
*/